/*
 * HtmlHandler.java
 *
 * Created on March 24, 2008, 6:08 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package com.afaker.searth.core.index.handler;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.w3c.tidy.Configuration;
import org.w3c.tidy.Tidy;

/**
 *
 * @author bruce
 */
public class HtmlHandler extends DocumentHandler {

    private Tidy tidy;

    /**
     * Creates a new instance of HtmlHandler
     */
    public HtmlHandler() {
        tidy = new Tidy();
        tidy.setQuiet(true);
        tidy.setShowWarnings(false);
        tidy.setCharEncoding(Configuration.UTF8);
    }

    public Document getDocument(File file) {
        org.w3c.dom.Document root = null;
        try {
            root = tidy.parseDOM(new FileInputStream(file), null);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        Element rawDoc = root.getDocumentElement();
        org.apache.lucene.document.Document doc =
                new org.apache.lucene.document.Document();
        String title = "" + getTitle(rawDoc);
        String body = "" + getBody(rawDoc);
        if (body == null || body.trim().equals("")) {
            return null;
        }
        doc.add(new Field("path", file.getAbsolutePath(),
                Field.Store.YES, Field.Index.TOKENIZED));
        doc.add(new Field("content", new StringReader(file.getAbsolutePath() +
                body.trim() +
                title.trim())));
        String size = String.valueOf(file.length() / 1024.0) + "KB";
        float s = file.length() / 1024.0f / 1024.0f;
        float l = file.length() / 1024.0f;
        if (s >= 1) {
            size = String.valueOf(s) + "M";
        } else {
            size = String.valueOf(l) + "KB";
        }
        doc.add(new Field("format", "html", Field.Store.YES, Field.Index.TOKENIZED));
        doc.add(new Field("size", size, Field.Store.YES, Field.Index.UN_TOKENIZED));
        doc.add(new Field("modified", String.valueOf(file.lastModified()),
                //DateTools.timeToString(file.lastModified(), DateTools.Resolution.MINUTE),
                Field.Store.YES, Field.Index.UN_TOKENIZED));
        return doc;
    }

    protected String getTitle(Element rawDoc) {
        if (rawDoc == null) {
            return null;
        }
        String title = "";
        NodeList children = rawDoc.getElementsByTagName("title");
        if (children.getLength() > 0) {
            Element titleElement = ((Element) children.item(0));
            Text text = (Text) titleElement.getFirstChild();
            if (text != null) {
                title = text.getData();
            }
        }
        return title;
    }

    protected String getBody(Element rawDoc) {
        if (rawDoc == null) {
            return null;
        }
        String body = "";
        NodeList children = rawDoc.getElementsByTagName("body");
        if (children.getLength() > 0) {
            body = getText(children.item(0));
        }
        return body;
    }

    protected String getText(Node node) {
        NodeList children = node.getChildNodes();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            switch (child.getNodeType()) {
                case Node.ELEMENT_NODE:
                    sb.append(getText(child));
                    sb.append(" ");
                    break;
                case Node.TEXT_NODE:
                    sb.append(((Text) child).getData());
                    break;
            }
        }
        return sb.toString();
    }

    public static void main(String args[]) throws Exception {
        HtmlHandler handler = new HtmlHandler();
        org.apache.lucene.document.Document doc = handler.getDocument(
                new File("/home/bruce/Desktop/Web Page/Struts 2 + GWT.htm"));
        System.out.println(doc);
    }
}
